<template>
  <div class="admin">
    <el-breadcrumb separator="/" class="mianbao">
      <el-breadcrumb-item :to="{ path: '/index' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>账号管理</el-breadcrumb-item>
      <el-breadcrumb-item>管理员列表</el-breadcrumb-item>
    </el-breadcrumb>

    <div class="flex">
      <el-button type="primary" @click="toadminadd()">
        添加管理员 &nbsp;<el-icon><Plus /></el-icon>
      </el-button>
    </div>

    <el-table
      v-loading="loading"
      border
      stripe
      :data="tableData"
      style="width: 100%"
    >
      <el-table-column label="序号" width="80" type="index" />
      <el-table-column label="账号" prop="adminname"></el-table-column>
      <el-table-column label="权限">
        <template #default="scope">
          <el-tag type="success">
            {{ scope.row.role == 1 ? "管理员" : "超级管理员" }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template #default="scope">
          <el-button @click="toEditAdd(scope.row)">编辑</el-button>
          <el-button type="danger" @click="removeadmin(scope.row.adminid)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <el-drawer
      v-model="drawer"
      :title="ruleForm.adminid ? '编辑管理员' : '新增管理员'"
      :before-close="handleClose"
    >
      <el-form
        ref="ruleFormRef"
        :model="ruleForm"
        :rules="rules"
        label-width="60px"
        class="form"
        status-icon
      >
        <el-form-item label="账号" prop="adminname">
          <el-input v-model="ruleForm.adminname" :disabled="ruleForm.adminid?true:false"/>
        </el-form-item>
        <el-form-item label="密码" prop="password" >
          <el-input v-model="ruleForm.password" :disabled="ruleForm.adminid?true:false"/>
        </el-form-item>
        <el-form-item label="角色" prop="role">
          <el-select
            style="width: 400px"
            v-model="ruleForm.role"
            placeholder="请选择"
          >
            <el-option label="管理员" :value="1" />
            <el-option label="超级管理员" :value="2" />
          </el-select>
        </el-form-item>
        <el-form-item label="权限" prop="checkedKeys">
          <el-tree
            ref="tree"
            @check="handleCheck"
            :data="data"
            show-checkbox
            node-key="id"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="adminAdd(ruleFormRef)">
            {{ ruleForm.adminid ? "编辑管理员" : "新增管理员" }}
          </el-button>
        </el-form-item>
      </el-form>
    </el-drawer>
  </div>
</template>

<script setup>
import { ref, onMounted ,nextTick} from "vue";
import { adminlist, admindelete, adminadd, adminupdate } from "@/api/index";
import { ElNotification } from "element-plus";
import "element-plus/es/components/notification/style/css";
var loading = ref(false);
var tableData = ref([]);
var drawer = ref(false);
var ruleFormRef = ref();
var tree = ref();
var ruleForm = ref({
  adminname: "",
  password: "",
  role: "",
  checkedKeys: "",
});
var data = ref([
  {
    id: "1",
    label: "首页",
  },
  {
    id: "2",
    label: "轮播图管理",
    children: [
      {
        id: "2-1",
        label: "轮播图列表",
      },
    ],
  },
  {
    id: "3",
    label: "账号管理",
    children: [
      {
        id: "3-1",
        label: "管理员列表",
      },
    ],
  },
  {
    id: "4",
    label: "产品管理",
    children: [
      {
        id: "4-1",
        label: "产品列表",
      },
      {
        id: "4-2",
        label: "秒杀列表",
      },
      {
        id: "4-3",
        label: "推荐列表",
      },
      {
        id: "4-4",
        label: "筛选列表",
      },
    ],
  },
  {
    id: "5",
    label: "数据可视化",
    children: [
      {
        id: "5-1",
        label: "echarts使用",
      },
    ],
  },
  {
    id: "6",
    label: "编辑器",
    children: [
      {
        id: "6-1",
        label: "编辑器使用",
      },
    ],
  },
  {
    id: "7",
    label: "excel管理",
    children: [
      {
        id: "7-1",
        label: "excel使用",
      },
    ],
  },
  {
    id: "8",
    label: "地图管理",
    children: [
      {
        id: "8-1",
        label: "百度地图",
      },
    ],
  },
]);
var rules = ref({
  adminname: [{ required: true, message: "请输入账号", trigger: "blur" }],
  password: [{ required: true, message: "请输入密码", trigger: "blur" }],
  role: [{ required: true, message: "请选择角色", trigger: "blur" }],
  checkedKeys: [{ required: true, message: "请选择权限", trigger: "blur" }],
});
var handleCheck = (prop1, { checkedKeys }) => {
  //勾选状态保存
  ruleForm.value.checkedKeys = checkedKeys;
};
var handleClose = (done) => {
  //清空表单和树控件
  ruleForm.value = {
    adminid: "",
    adminname: "",
    password: "",
    role: "",
    checkedKeys: "",
  };
  tree.value.setCheckedKeys([]);
  done();
};
var render = async () => {
  loading.value = true;
  var ok = await adminlist();
  console.log(ok);
  tableData.value = ok.data.data;
  loading.value = false;
};
onMounted(() => {
  render();
});

var removeadmin = async (adminid) => {
  var ok = await admindelete({ adminid });
  console.log(ok);
  if (ok.data.code == 200) {
    ElNotification({
      title: "管理员删除通知",
      message: "删除成功",
      type: "success",
      duration: 1500,
    });
    var i = tableData.value.findIndex((item) => item.adminid == adminid);
    if (i != -1) tableData.value.splice(i, 1);
  }
};
var toadminadd = () => {
  drawer.value = true;
};
var toEditAdd = (admin) => {
  drawer.value = true;
  ruleForm.value = admin;
  nextTick(()=>{
    tree.value.setCheckedKeys(admin.checkedKeys)
  })
};
var adminAdd = () => {
  ruleFormRef.value.validate(async (valid) => {
    if (valid) {
      console.log(ruleForm.value);
      if (ruleForm.value.adminid) {
        var ok = await adminupdate(ruleForm.value);
        if (ok.data.code == 200) {
          ElNotification({
            title: "编辑通知",
            message: "编辑管理员成功!",
            type: "success",
            duration: 1500,
          });
          drawer.value = false;
          ruleForm.value = {
            adminid: "",
            adminname: "",
            password: "",
            role: "",
            checkedKeys: "",
          };
          tree.value.setCheckedKeys([]);
        } else {
          ElNotification({
            title: "编辑通知",
            message: "编辑管理员失败!",
            type: "error",
            duration: 1500,
          });
        }
      } else {
        //新增管理员
        var ok = await adminadd(ruleForm.value);
        //  console.log(ok);
        if (ok.data.code == 200) {
          ElNotification({
            title: "新增通知",
            message: "新增管理员成功!",
            type: "success",
            duration: 1000,
          });
          drawer.value = false;
          ruleForm.value = {
            adminid: "",
            adminname: "",
            password: "",
            role: "",
            checkedKeys: "",
          };
          tree.value.setCheckedKeys([]);
          render();
        } else {
          ElNotification({
            title: "新增通知",
            message: "新增管理员失败!",
            type: "error",
            duration: 1000,
          });
        }
      }
    }
  });
};
</script>

<style scoped lang='scss'>
.flex {
  margin: 20px 0;
}
.admin {
  .form {
    max-width: 400px;
  }
}
</style>